package ComDirectBehaviour;

import java.io.IOException;

import Saboteur.AgentSaboteur;

import java.util.ArrayList;
import java.util.HashMap;

import jade.core.AID;
import jade.core.behaviours.Behaviour;
import jade.lang.acl.ACLMessage;
import jade.lang.acl.UnreadableException;
import Agent.AgentBase;
import Goal.GoalBase;
import Graph.Arete;
import Graph.Graph;
import Graph.Noeud;
import Message.MEdge;
import Message.MUpdateArete;
import Perception.PerceptsBase;
import Perception.Percepts;
/**
 * TODO description
 */
public class ComInfoSab extends Behaviour {

	
	private Graph graph;
	
	public void init(String name,String teamName,PerceptsBase percepts, GoalBase goals,AID aids[], boolean verbose)
	{
		original(name,teamName,percepts,goals,aids,verbose);
		graph = ((AgentSaboteur)getAgent()).getGraph();
		
	}
	
	public void checkMessage(ACLMessage msg)
	{
		original(msg);
		checkInfo(msg);
	}

	public void action()
	{
		updateGraph();
		original();
	}
	
	public void checkGraph(ACLMessage msg)
	{
		String id = msg.getConversationId();
		AgentBase ag = (AgentBase)(getAgent());
		if(id.equals("Noeud"))
		{
			MEdge edge = null;
			try
			{
			   edge =  (MEdge)(msg.getContentObject());
			}
			catch(Exception e)
			{
				System.err.println(e.getMessage());
				e.printStackTrace();
				return;
			}
			String s1= edge.getNode1();
			String s2 = edge.getNode2();
			
			Noeud n1 = null;
			Noeud n2 = null;
			
			if(!graph.isPresent(s1))
			{
				n1 = new Noeud(s1);
				graph.addNoeud(n1);
			}
			else
				n1 = graph.getNoeud(s1);
			boolean find = false;
			
			for(Arete a: n1.getAretes())
				if(a.getArrive().getName().equals(s2))
				{
					find = true;
					break;
				}
			
			if(!find)
			{
				if(ag.isVerbose())
					ag.println("  Add Noeud --> "+msg.getContent());
				if(!graph.isPresent(s2))
				{
					n2 = new Noeud(s2);
					n1.addNoeud(n2,1);
					graph.addNoeud(n2);
				}
				else
					n1.addNoeud(graph.getNoeud(s2),1);
			}
					
		}
		else if(id.equals("Update"))
		{
			MUpdateArete arete = null;
			try
			{
				arete = (MUpdateArete)(msg.getContentObject());
							}
			catch(Exception e)
			{
				System.err.println(e.getMessage());
				e.printStackTrace();
				return;
			}
			String s1= arete.getNoeud1();
			String s2 = arete.getNoeud2();
			int poids = arete.getPoids();
			
			Noeud n1 = null;
			Noeud n2 = null;
			
			if(!graph.isPresent(s1))
			{
				n1 = new Noeud(s1);
				graph.addNoeud(n1);
			}
			else
				n1 = graph.getNoeud(s1);
			boolean find = false;
			
			for(Arete a: n1.getAretes())
				if(a.getArrive().getName().equals(s2))
				{
					find = true;
					break;
				}
			
			if(!find)
			{
				if(ag.isVerbose())
					ag.println("  Add Noeud --> "+msg.getContent());
				if(!graph.isPresent(s2))
				{
					n2 = new Noeud(s2);
					n1.addNoeud(n2,1);
					graph.addNoeud(n2);
				}
				else
					n1.addNoeud(graph.getNoeud(s2),1);
			}
			Arete a1 = null;
			for(Arete a: n1.getAretes())
				if(a.getArrive().getName().equals(s2))
				{
					a1 =a;
				}
			a1.setPoids(poids);
		}
		
	}

	public void updateGraph()
	{
		if(percepts.perceptsExists("visibleEdge"))
		{
			for(Percepts p : percepts.getPercepts("visibleEdge"))
			{
				String vertex1 = (String)(p.getParam("node1").getValue());
				String vertex2 = (String)(p.getParam("node2").getValue());
				
				Noeud ver1, ver2;
				
				if(!graph.isPresent(vertex1))
				{
					ver1 = new Noeud(vertex1);
					graph.addNoeud(ver1);
					
				}
				else
				{
					ver1 = graph.getNoeud(vertex1);
				}
				if(!graph.isPresent(vertex2))
				{
					goals.addGoal("EdgeAVis-"+vertex2);
					ver2 = new Noeud(vertex2);
					graph.addNoeud(ver2);
				}
				else
				{
					ver2 = graph.getNoeud(vertex2);
				}		
				
				boolean add = true;
				
				for(Arete a : ver1.getAretes())
				{
					if(a.getArrive() == ver2 )
						add = false;
				}
				if(add)
				{
					ver1.addNoeud(ver2, 1);
				}
			}
					
		}
			
	}
}